import sys
from PyQt5.QtCore import QSize, Qt
from PyQt5.QtWidgets import (
    QApplication,
    QMainWindow, 
    QLabel,
    QLineEdit,
    QVBoxLayout,
    QWidget,
    QPushButton)



# Formula - P(A) = (n / m) ** count
n = 1 # Кол-во Благоприятных исходов
m = 2 # Общее кол-во исходов
result_orel_str = "Вероятность что выпадет орел: "
result_reshka_str = "Вероятность что выпадет решка: "
title_text = "Введите количество выпаданий орла и решки"

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        # sets 
        self.setWindowTitle('Орел и Решка')
        self.setFixedSize(QSize(500,300))

        # Widgets
        self.title= QLabel(title_text)
        self.result_orel= QLabel(result_orel_str)
        self.result_reshka = QLabel(result_reshka_str)
        self.input_orel = QLineEdit()
        self.input_reshka = QLineEdit()
        self.button = QPushButton("Расчитать вероятность")

        # Widgets styles
        self.title.setAlignment(Qt.AlignCenter)
        self.title.setStyleSheet("QLabel { font-size: 18px;}")
        self.result_orel.setStyleSheet("QLabel {margin: 0px; font-size: 15px; }")
        self.result_reshka.setStyleSheet("QLabel {margin: 0px; font-size: 15px; }")
        self.input_orel.setStyleSheet("QLineEdit {padding: 10px; font-size: 18px; }")
        self.input_reshka.setStyleSheet("QLineEdit {padding: 10px; font-size: 18px; }")
        self.button.setStyleSheet("QPushButton {padding: 10px; font-size: 14px; }")

        # Events
        self.button.clicked.connect(self.printResult)

        # Layout
        boxLayout = QVBoxLayout()
        boxLayout.addWidget(self.title)
        boxLayout.addWidget(self.input_orel)
        boxLayout.addWidget(self.input_reshka)
        boxLayout.addWidget(self.result_orel)
        boxLayout.addWidget(self.result_reshka)
        boxLayout.addWidget(self.button)

        # containers
        container = QWidget()
        container.setLayout(boxLayout)
        
        self.setCentralWidget(container)

    def printResult(self, e):
        try:
            count_orel = int(self.input_orel.text())
            count_reshka = int(self.input_reshka.text())

            if count_orel == count_reshka:
                result_orel = result_orel_str + "50%"
                result_reshka = result_reshka_str + "50%"
            else:
                result_orel = result_orel_str + str( int(float( (n/m)**count_orel ) * 100) ) + '%'
                result_reshka = result_reshka_str + str( int(float( (n/m)**count_reshka ) * 100) ) + '%'

            self.title.setText(title_text)
            self.title.setStyleSheet("QLabel { font-size: 18px;}")
            self.result_orel.setText(result_orel)
            self.result_reshka.setText(result_reshka)

        except:
            self.title.setText('Ошибка, писать только цифры!')
            self.title.setStyleSheet("QLabel { background-color: red; font-size: 18px;}")
            self.result_orel.setText('')
            self.result_reshka.setText('')
            return False

 
app = QApplication(sys.argv)

window = MainWindow()
window.show()

app.exec()
